var RunLayer = cc.Layer.extend({
	arr:null,
	scene:null,
	clock:null,
	ctor:function (parent) {
		this._super();
		this.scene = parent;
		this.scene.addChild(this, 10);
		gg.main = this;
		this.init();
	},
	init:function () {
		this.callNext = cc.callFunc(function(){
			gg.flow.next();
		}, this);
		this.callKill = cc.callFunc(function(p){
			var seq = cc.sequence(cc.fadeOut(0.5),cc.callFunc(function(){
				p.removeFromParent(true);	
			}, this));
			p.runAction(seq);
		}, this); 
		
		this.callNext.retain();
		this.callKill.retain();
		this.clock = new Clock(this);
		// 天平
		this.balance = new Balance(this);
		// 左物
		var paper = new Button(this, 10, TAG_PAPER, "#paper.png", this.callback);
		paper.setPosition(920,290);
		// 物品库
		this.lib = new Lib(this);
		
		this.scheduleOnce(function(){
// this.loadTest();
		},1);
	},
	loadTest:function(){
 this.lib.moveLib(TAG_LIB_SALT,400);
// ll.run.loadBeaker(cc.p(900,900));
// ss = ll.run.getChildByTag(TAG_BEAKER4);
	},
	test2:function(){
		var move = cc.moveTo(0.7,cc.p(570,320));
		var rotate = cc.rotateTo(0.8, 90);
		var func = cc.callFunc(function(){
			ss.pour2();
			ss.hide(3, true);
		},this);
		var seq = cc.sequence(move, rotate, func);
		ss.runAction(seq);
	},
	loadSalt:function(pos){
		this.salt = new SampleBottle(this);
		this.addChild(this.salt, 20);
		this.loadInLib(this.salt, pos, cc.p(600, 250));
	},
	loadCyl:function(pos){
		var cylinder = new Button(this, 10, TAG_CYLINDER,"#cylinder.png", this.callback);
		this.loadInLib(cylinder, pos, cc.p(280,150));
	},
	loadKettle:function(pos){
		var kettle = new Button(this, 10, TAG_KETTLE,"#kettle.png", this.callback);
		this.loadInLib(kettle, pos, cc.p(700,200));
	},
	loadIron:function(pos){
		this.ironNode = new cc.Node();
		this.ironNode.setCascadeOpacityEnabled(true);
		this.addChild(this.ironNode,8);
		
		var iron = new Button(this.ironNode, 10, TAG_IRON, "#ironShelf.png");
		this.loadInLib(iron, pos, cc.p(200, 300));
	},
	loadBeaker:function(pos){
		var action = null;
		if(gg.flow.flow != null){
			action = gg.flow.flow.action;
		}
		if(action == ACTION_DO1){
			var beaker = new Beaker(this, 10, TAG_BEAKER, this.callback); 
			this.loadInLib(beaker, pos, cc.p(550, 140));
		} else if(action == ACTION_DO2){
			var beaker = new Beaker(this, 10, TAG_BEAKER2, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		} else if(action == ACTION_DO3){
			var beaker = new Beaker(this, 10, TAG_BEAKER3, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		} else if(action == ACTION_NONE){
			var beaker = new Beaker(this, 10, TAG_BEAKER4, this.callback); 
			this.loadInLib(beaker, pos, cc.p(180, 185));
		}
	},
	loadFunnel:function(pos){
		var funnel = new Button(this.ironNode, 11, TAG_FUNNEL, "#funnel.png");
		this.loadInLib(funnel, pos, cc.p(200, 250));
	},
	showPaper:function(pos){
		var filter = new Win(this,"#filter_paper.png");
		filter.show(true);
	},
	showPaper2:function(pos){
		var filter = new Win(this,"#paper_funnel.png");
		filter.show(false);
	},
	loadPaper:function(pos){
		if(pos == null){
			pos = this.lib.getPosition();
		}
		var paper = new cc.Sprite("#filter_paper2.png");
		this.ironNode.addChild(paper, 20);
		this.loadInLib(paper, pos, cc.p(200, 285));
	},
	loadPaper2:function(pos){
		var paper = new Button(this, 7, TAG_PAPER2, "#paper.png",this.callback);
		this.loadInLib(paper, pos, cc.p(700, 220));
	},
	loadRod:function(pos){
		var rod = new Button(this, 12, TAG_ROD, "#rod.png",this.callback);
		this.loadInLib(rod, pos, cc.p(400, 400), 1);
	},
	loadSpoon:function(pos){
		var spoon = new Button(this, 11, TAG_SPOON, "#spoon3.png",this.callback);
		this.loadInLib(spoon, pos, cc.p(700, 400), 1);
	},
	loadAir:function(pos){
		var air = new Button(this, 11, TAG_AIR, "#air.png",this.callback);
		var airFunnel = new Button(air, 7, TAG_AIR_FUNNEL, "#air_funnel.png",this.callback, this);
		airFunnel.setPosition(263, 165);
		this.loadInLib(air, pos, cc.p(250, 250), 1);
	},
	loadAirSide:function(){
		var airSide = new AirSize(this);
	},
	loadLamp:function(pos){
		// 650,190
		var lamp = new Lamp(this);
		lamp.setPosition(pos);
		var ber = $.bezier(lamp.getPosition(), cc.p(800,190), 1);
		var seq = cc.sequence(ber, this.callNext);
		lamp.runAction(seq);
	},
	loadMatch:function(pos){
		var match = new Match(this);
		match.setPosition(pos);
		var ber = $.bezier(match.getPosition(), cc.p(750,390), 1);
		var seq = cc.sequence(ber, this.callNext);
		match.runAction(seq);
	},
	loadShelf:function(pos){
		this.lampNode = new cc.Node();
		this.lampNode.setCascadeOpacityEnabled(true);
		this.addChild(this.lampNode,8,TAG_LAMP_NODE);
		
		var shelf = new cc.Sprite("#shelf.png");
		this.lampNode.addChild(shelf,3);
		this.loadInLib(shelf, pos, cc.p(650,200), 1);
	},
	loadNet:function(pos){
		var net = new cc.Sprite("#asbestosNet.png");
		this.lampNode.addChild(net,4);
		this.loadInLib(net, pos, cc.p(645,255), 1);
	},
	loadDrop:function(pos){
		var tag = null;
		var action = gg.flow.flow.action;
		if(action == ACTION_DO1){
			tag = TAG_BACL;
		} else if(action == ACTION_DO2){
			tag = TAG_NAOH;
		} else if(action == ACTION_DO3){
			tag = TAG_NA2CO3;
		} else if(action == ACTION_DO4){
			tag = TAG_HCL;
		} else if(action == ACTION_NONE){
			tag = TAG_ALCOHOL;
		}
		var drop = new DropBottle(this, tag);
		this.loadInLib(drop, pos, cc.p(480,250), 1);
	},
	loadPot:function(pos){
		var pot = new Pot(this);
		pot.setPosition(pos);
		this.loadInLib(pot, pos, cc.p(645,275), 1);
	},
	loadPH:function(pos){
		var ph = new PhCard(this);
		ph.setPosition(pos);
		this.loadInLib(ph, pos, cc.p(500,400), 1);
	},
	loadWide:function(pos){
		var wide = new WideBottle(this);
		this.loadInLib(wide, pos, cc.p(600, 200), 1);
	},
	loadInLib:function(obj, pos, tarPos,delay){
		obj.setPosition(pos);
		if(delay == null){
			delay = 1;
		}
		var ber = $.bezier(pos, tarPos, delay);
		var seq = cc.sequence(ber, this.callNext);
		obj.runAction(seq);
	},
	kill:function(obj){
		var fade = cc.fadeTo(0.5,0);
		var func = cc.callfunc(function(){
			obj.removeFromParent(true);
		},this);
		var seq = cc.sequence(fade,func);
		obj.runAction(seq)
	},
	callback:function (p){
		var func = cc.callFunc(this.actionDone, this);
		switch(p.getTag()){
			case TAG_PAPER:
				var move = cc.moveTo(1,cc.p(520,220));
				var rotate = cc.rotateTo(0.5, 40);
				var seq = cc.sequence(move,rotate,func);
				p.runAction(seq);
				break;
			case TAG_KETTLE:
				var ber = cc.bezierTo(1, [cc.p(480,250),cc.p(450,220),cc.p(400,230)]);
				var rotate = cc.rotateTo(1, -60);
				var spawn = cc.spawn(ber,rotate);
				var delay = cc.delayTime(1.7);
				var moveBack = cc.moveTo(1,cc.p(700,200));
				var rotate = cc.rotateTo(1, 0);
				var spaw2 = cc.spawn(moveBack,rotate);
				var seq = cc.sequence(spawn, func,delay,spaw2,this.callKill);
				p.runAction(seq);
				break;
			case TAG_CYLINDER:
				var move = cc.moveTo(1,cc.p(460,170));
				var rotate = cc.rotateTo(1, 60);
				var spawn = cc.spawn(move,rotate);
				var delay = cc.delayTime(2);
				var move2 = cc.moveTo(1,cc.p(480,150));
				var rotate2 = cc.rotateTo(1, 0);
				var spawn2 = cc.spawn(move2,rotate2);
				var seq = cc.sequence(spawn, func, delay, spawn2,this.callKill);
				p.runAction(seq);
				break;
			case TAG_BEAKER:
				this.beakerYinliu(p, TAG_BEAKER2);
				break;
			case TAG_BEAKER2:
				if(gg.flow.flow.action == ACTION_DO1){
					this.beakerToNet(p);
				} else if(gg.flow.flow.action == ACTION_DO2){
					this.beakerYinliu(p, TAG_BEAKER3);
				}
				break;
			case TAG_BEAKER3:
				if(gg.flow.flow.action == ACTION_DO1){
					this.beakerToNet(p);
				} else if(gg.flow.flow.action == ACTION_DO2){
					this.beakerYinliu(p, TAG_BEAKER4);
				}
				break;
			case TAG_BEAKER4:
				if(gg.flow.flow.action == ACTION_DO1){
					var move = cc.moveTo(1,cc.p(350, 250));
					var seq = cc.sequence(move, this.callNext);
					p.runAction(seq);
				} else if(gg.flow.flow.action == ACTION_DO2){
					// 倒到坩埚
					var move = cc.moveTo(0.7,cc.p(570,320));
					var rotate = cc.rotateTo(0.8, 90);
					var func = cc.callFunc(function(){
						p.pour2();
						p.hide(3, true);
					},this);
					var seq = cc.sequence(move, func, rotate);
					p.runAction(seq);
					// 隐藏铁架台
					if(this.ironNode != null){
						this.ironNode.runAction(cc.fadeTo(0.5,0));	
					}
				}
				break;
			case TAG_ROD:
				if(gg.flow.flow.action == ACTION_DO1){
					var rotate = cc.rotateTo(0.3, 45);
					var ber = cc.bezierTo(1.5, [cc.p(480,500),cc.p(600,200),cc.p(535, 180)]);
					var func2 = cc.callFunc(function(){
						// 搅拌，产生波纹
						var beaker = this.getChildByTag(TAG_BEAKER);
						beaker.stir(2);
					}, this);
					var moveSeq = cc.sequence(cc.moveBy(0.2,cc.p(35, 0)),cc.moveBy(0.2,cc.p(-35, 0)));
					var repeat = cc.repeat(moveSeq, 3);
					var ber2 = cc.bezierTo(0.7, [cc.p(600,200),cc.p(480,500),cc.p(400, 400)]);
					var rotate2 = cc.rotateTo(0.3, -45);
					var seq = cc.sequence(rotate, ber, func2, repeat, func, ber2, rotate2, this.callKill);
					p.runAction(seq);
				} else if(gg.flow.flow.action == ACTION_DO2){
					 var rotate = cc.rotateTo(0.3, 20);
					 var move = cc.moveTo(0.7,cc.p(185,355));
					 var delay = cc.delayTime(1);
					 var seq = cc.sequence(rotate, move, func, delay, this.callNext);
					 p.runAction(seq);
				} else if(gg.flow.flow.action == ACTION_DO3){
					// 蒸发名冒烟
					this.getChildByTag(TAG_POT_NODE).smoking();
					// 搅拌浓缩,蒸发
					var rotate = cc.rotateTo(0.3, 45);
					var move = cc.moveTo(0.7,cc.p(620, 355));
					var moveSeq = cc.sequence(cc.moveBy(0.2,cc.p(60, 0)),cc.moveBy(0.2,cc.p(-60, 0)));
					var repeat = cc.repeat(moveSeq, 5);
					var move2 = cc.moveTo(0.7,cc.p(400,400));
					var rotate2 = cc.rotateTo(0.3, -45);
					var seq = cc.sequence(rotate,move, repeat, move2, rotate2, this.callNext,this.callKill);
					p.runAction(seq);
				} else if(gg.flow.flow.action == ACTION_DO5){
					var rotate = cc.rotateTo(0.3, 45);
					var move = cc.moveTo(0.7,cc.p(360, 320));
					var ber = cc.bezierTo(1, [cc.p(300,500),cc.p(360,500),cc.p(360, 320)]);
					var delay = cc.delayTime(0.3);
					var move2 = cc.moveTo(0.7,cc.p(500,440));
					var move3 = cc.moveTo(0.7,cc.p(400,400));
					var rotate3 = cc.rotateTo(0.3, -45);
					var seq = cc.sequence(rotate, ber, delay, move2, delay, func, move3, rotate3, this.callNext, this.callKill);
					p.runAction(seq);
				}
			case TAG_SPOON:
				
				break;
			case TAG_AIR:
				this.loadAirSide();
				gg.flow.next();
				break;
			case TAG_PAPER2:
				if (gg.flow.flow.action == ACTION_DO1) {
					var move = cc.moveTo(0.5,cc.p(920,290));
					var seq = cc.sequence(move, func);
					p.runAction(seq);	
				} else if(gg.flow.flow.action == ACTION_DO2) {
					var move = cc.moveTo(0.5, cc.p(560, 280));
					var rotate = cc.rotateTo(0.5, 30);
					var delay = cc.delayTime(0.3);
					var fade = cc.fadeOut(0.5);
					var seq = cc.sequence(move, rotate, func, delay, fade);
					p.runAction(seq);	
				}
				break;
			case TAG_AIR_FUNNEL:
				var move = cc.moveTo(0.5, cc.p(530, 60));
				var rotate = cc.rotateTo(0.3,45);
				var rotate2 = cc.rotateTo(0.3,0);
				var move2 = cc.moveTo(0.5, cc.p(263, 165));
				var delay = cc.delayTime(3);
				var seq = cc.sequence(move , rotate, func, delay, rotate2, move2, this.callNext);
				p.runAction(seq);
				break;
		}
	},
	beakerToNet:/**
				 * 把烧杯挪到石棉网
				 */
	function(p){
		var move = cc.moveTo(0.6, cc.p(645, 290));
		var seq = cc.sequence(move, this.callNext);
		p.runAction(seq);
	},
	beakerYinliu:/**
					 * 烧杯引流
					 */
	function(p, tag){
		// 通过引流棒过滤
		var bezier = cc.bezierTo(1, [cc.p(520,340),cc.p(385,540),cc.p(228,375)]);
		var rotate = cc.rotateTo(0.8, -90);
		var seq = cc.sequence(bezier,cc.callFunc(function(){
			// 烧杯水位线下降
			p.pour();
			p.hide(3);
			// 烧杯水位线上升
			var beaker = this.getChildByTag(tag);
			beaker.upLine(true);
		}, this),rotate);
		p.runAction(seq);
	},
	actionDone:function(p){
		var func = cc.callFunc(this.actionDone, this);
		switch(p.getTag()){
			case TAG_PAPER:
				p.runAction(cc.fadeTo(0.5, 0));
				var keaber = this.getChildByTag(TAG_BEAKER);
				keaber.showSalt(true);
				break;
			case TAG_KETTLE:
				var cylinder = this.getChildByTag(TAG_CYLINDER);
				cylinder.setCascadeOpacityEnabled(true);
				var clyDtl = new cc.Sprite("#cly_dtl.png");
				clyDtl.setPosition(cylinder.width + clyDtl.width * 0.5,cylinder.height * 0.5);
				clyDtl.setOpacity(0);
				clyDtl.setCascadeOpacityEnabled(true);
				cylinder.addChild(clyDtl);
				var fade1 = cc.fadeTo(0.5, 255);
				var delay = cc.delayTime(1.5);
				var fade2 = cc.fadeTo(0.5,0);
				var sequence = cc.sequence(fade1, delay, fade2,cc.callFunc(this.flowNext, this));
				clyDtl.runAction(sequence);
				var clyLine = new cc.Sprite("#cly_line.png");
				clyDtl.addChild(clyLine);
				clyLine.setPosition(clyDtl.width * 0.5, 0);
				clyLine.runAction(cc.moveBy(1.5,cc.p(0,clyDtl.height * 0.8)));
				
				var clyLine2 = new cc.Sprite("#cyl.png");
				cylinder.addChild(clyLine2, 1, TAG_CYL_LINE);
				clyLine2.setPosition(cylinder.width * 0.5, 10);
				clyLine2.runAction(cc.moveBy(1.5,cc.p(0,cylinder.height * 0.4)));
				// 水流
				var flowRight = new cc.Sprite("#flow_right.png");
				flowRight.setAnchorPoint(1, 1);
				flowRight.setScale(0);
				flowRight.setRotation(60);
				flowRight.setPosition(10, 220);
				p.addChild(flowRight);
				var seq = cc.sequence(cc.scaleTo(0.5,1),cc.delayTime(1),cc.scaleTo(0.2,0));
				flowRight.runAction(seq);
				break;
			case TAG_CYLINDER:
				// 导入30ml的水
				var beaker = this.getChildByTag(TAG_BEAKER);
				beaker.upLine(true);
				var cylLine = p.getChildByTag(TAG_CYL_LINE);
				if(cylLine != null){
					cylLine.runAction(cc.moveBy(1.5,cc.p(0, -p.height * 0.4 + 10)));	
				}
				// 水流
				var flowLeft = new cc.Sprite("#flow_left.png");
				flowLeft.setAnchorPoint(0, 1);
				flowLeft.setScale(0);
				flowLeft.setRotation(-60);
				flowLeft.setPosition(25, 174);
				p.addChild(flowLeft);
				var seq = cc.sequence(cc.scaleTo(0.5,1),cc.delayTime(1.3),cc.scaleTo(0.2,0));
				flowLeft.runAction(seq);
				break;
			case TAG_ROD:
				if(gg.flow.flow.action == ACTION_DO1){
					var beaker = this.getChildByTag(TAG_BEAKER);
					// 沉淀消失
					beaker.waitDiss(true);
				} else if(gg.flow.flow.action == ACTION_DO2){
					this.showPaper2();
				} else if(gg.flow.flow.action == ACTION_DO5){
					var ph = this.getChildByTag(TAG_PH_NODE);
					ph.changeColor();
				}
				break;
			case TAG_PAPER2:
				// 展示重量，游戏结束
				if(gg.flow.flow.action == ACTION_DO1){
					this.balance.showDisk();
				} else if(gg.flow.flow.action == ACTION_DO2){
					var wide = this.getChildByTag(TAG_WIDE_NODE);
					wide.addSalt(true);
				}
				break;
			case TAG_AIR_FUNNEL:
				var spoon = this.getChildByTag(TAG_SPOON);
				var move = cc.moveTo(0.5,cc.p(710,280));
				var moveSeq = cc.sequence(cc.moveBy(0.3,cc.p(-30, -25)),cc.moveBy(0.3,cc.p(30, 25)));
				var repeat = cc.repeat(moveSeq, 3);
				var delay = cc.delayTime(0.5);
				var move2 = cc.moveTo(0.5,cc.p(700,400));
				var seq = cc.sequence(move, moveSeq, repeat, func, delay, move2, this.callKill);
				spoon.runAction(seq);
				break;
			case TAG_SPOON:
				// 纸上出现NaCl晶体
				var paper = this.getChildByTag(TAG_PAPER2);
				var salt = new cc.Sprite("#salt5.png");
				salt.setPosition(50,25);
				salt.setOpacity(0);
				paper.setCascadeOpacityEnabled(true);
				paper.addChild(salt);
				salt.runAction(cc.fadeTo(0.5, 255));
				
				var bushi = this.getChildByTag(TAG_AIR).getChildByTag(TAG_AIR_FUNNEL).getChildByTag(TAG_BUSHI_SALT);
				if(bushi != null){
					bushi.runAction(cc.fadeOut(0.5));	
				}
				break;
		}
	},
	flowNext:function(){
		gg.flow.next();
	},
	onExit:function(){
		this._super();
		this.callNext.release();
		this.callKill.release();
	}
});